//
//  OrderCreatViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/18.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "OrderCreatViewController.h"

#import "ConsigneeTableViewCell.h"
#import "GoodsAmountTableViewCell.h"
#import "FundsTableViewCell.h"
#import "OrderAddrViewController.h"
#import "GoodsAttributeItem.h"
#import <UIImageView+AFNetworking.h>
#import "OrderModel.h"
#import "OrderPayViewController.h"


@interface OrderCreatViewController ()<UITableViewDelegate,UITableViewDataSource,OrderAddrDelegate,UITextFieldDelegate>{
    NSInteger count;
}
@property (nonatomic , weak) IBOutlet UITableView *tableview;
@property (nonatomic , weak) IBOutlet UIButton *orderBtn;

@property (weak, nonatomic) IBOutlet UILabel *aName;
@property (weak, nonatomic) IBOutlet UILabel *aTel;
@property (weak, nonatomic) IBOutlet UILabel *aAddress;

@property (weak, nonatomic) IBOutlet UIImageView *prodImageView;
@property (weak, nonatomic) IBOutlet UILabel *prodName;
@property (weak, nonatomic) IBOutlet UILabel *prodDes;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UILabel *prodPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalPricesLabel;
@property (weak, nonatomic) IBOutlet UILabel *payLabel;
@property (weak, nonatomic) IBOutlet UITextField *countField;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@end

@implementation OrderCreatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    count = 1;
    [self initProdInfo];
    // Do any additional setup after loading the view.
}

-(void)initProdInfo{
    self.prodName.text = _productItem.PRODUCT_NAME;
    self.prodDes.text = _productItem.PRODUCT_DESC;
    [self.prodImageView sd_setImageWithURL:[NSURL URLWithString:_productItem.PRODUCT_IMG_URL]];
    
    self.priceLabel.text = _RMB(_productItem.PRE_PRICE);
    
    self.prodPriceLabel.text = _RMB(_productItem.PRE_PRICE);
    
    self.totalPricesLabel.text = _RMB(_productItem.PRE_PRICE);
    
    self.payLabel.text = _RMB(_productItem.PRE_PRICE);
    
    self.countField.text = [NSString stringWithFormat:@"%d",count];
    self.countLabel.text = [NSString stringWithFormat:@"%d",count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)plusAction:(id)sender {
    count += 1;
    self.countField.text = [NSString stringWithFormat:@"%d",count];
    self.countLabel.text = [NSString stringWithFormat:@"%d",count];
    NSString *prices = [NSString stringWithFormat:@"%f",[_productItem.PRE_PRICE floatValue] * count];
    self.totalPricesLabel.text = _RMB(prices);
    self.payLabel.text = _RMB(prices);
}
- (IBAction)minusAction:(id)sender {
    count -= 1;
    if (count <= 1) {
        count = 1;
    }
     self.countField.text = [NSString stringWithFormat:@"%d",count];
    
    self.countLabel.text = [NSString stringWithFormat:@"%d",count];
    NSString *prices = [NSString stringWithFormat:@"%f",[_productItem.PRE_PRICE floatValue] * count];
    self.totalPricesLabel.text = _RMB(prices);
    self.payLabel.text = _RMB(prices);
    
}







-(void)selectedAddrWithName:(NSString *)name Tel:(NSString *)tel address:(NSString *)addr{
    self.aName.text = name;
    self.aTel.text = tel;
    self.aAddress.text = addr;
}

 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([[segue identifier] isEqualToString:@"address"]) {
         OrderAddrViewController *orderAddr = [segue destinationViewController];
         orderAddr.delegate = self;
     }
 }

- (IBAction)submitOrderAction:(id)sender {
    NSInteger price = [_productItem.PRE_PRICE floatValue] * count;
    [[OrderModel sharedModel] createOrderWithProdID:_productItem.PRODUCT_ID
                                          prodPrice:[NSString stringWithFormat:@"%d",price]
                                           prodName:_productItem.PRODUCT_NAME
                                            prodImg:_productItem.PRODUCT_IMG_URL
                                            prodNum:[NSString stringWithFormat:@"%d",count]
                                          buyerName:self.aName.text
                                         buyerPhone:self.aTel.text
                                          buyerAddr:self.aAddress.text
                                             result:^(BOOL success, NSString *orderId) {
                                                 NSLog(@"%@",orderId);
                                                 if (orderId != nil) {
                                                     OrderPayViewController *orderPayView = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderPayViewController"];
                                                     orderPayView.orderId = orderId;
                                                     orderPayView.payPrice = [NSString stringWithFormat:@"%d",price];
                                                     [self.navigationController pushViewController:orderPayView animated:YES];
                                                 }
                                             }];
}

#pragma mark - table delegate

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//}


@end
